package pl.poznan.put.cs.to.tests;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import pl.poznan.put.cs.to.tsp.common.FileOperations;
import pl.poznan.put.cs.to.tsp.common.IResultHolder;
import pl.poznan.put.cs.to.tsp.common.Vertexes;
import pl.poznan.put.cs.to.tsp.heuristics.NormalHeuristic;
import pl.poznan.put.cs.to.tsp.listeners.EmptyTSPListener;
import pl.poznan.put.cs.to.tsp.listeners.GraphicWindow;
import pl.poznan.put.cs.to.tsp.listeners.TSPListener;
import pl.poznan.put.cs.to.tsp.listeners.TSPWindowListener;
import pl.poznan.put.cs.to.tsp.localsearch.LocalSearch;

public class TimeLocalSearchTest {
	private static String fileName = "TSPA200.txt";
	private static Integer timeMilis = 90000;
	private static TSPListener listener = new TSPWindowListener(
			new GraphicWindow());
	private static TSPListener emptyListener = new EmptyTSPListener();
	private static IResultHolder best;
	private static Calendar cal = Calendar.getInstance();

	public static void main(String[] args) {
		Vertexes vertexes = FileOperations.loadInstance(fileName);
		IResultHolder localSearch;

		IResultHolder heuristic = new NormalHeuristic(vertexes, emptyListener);
		best = new LocalSearch(heuristic, listener);
		listener.solutionGenerated(best.getListA(), best.getListB(), vertexes);

		List<IResultHolder> results = new ArrayList<IResultHolder>();

		for (int i = 0; i < 1; i++) {
			System.out.println("=============");
			long start = Calendar.getInstance().getTimeInMillis();
			best = null;
			while (Calendar.getInstance().getTimeInMillis() - start < timeMilis) {
				heuristic = new NormalHeuristic(vertexes, emptyListener);
				localSearch = new LocalSearch(heuristic, emptyListener);

				if ((best == null)
						|| ((best.getLengthA() + best.getLengthB()) > (localSearch
								.getLengthA() + localSearch.getLengthB()))) {
					best = localSearch;
					listener.solutionGenerated(best.getListA(),
							best.getListB(), vertexes);
					System.out.println((best.getLengthA() + best.getLengthB()));
				}
			}
			results.add(best);
			System.out.println("=============");
		}

		best = null;
		for (IResultHolder irh : results) {
			if (best == null
					|| ((best.getLengthA() + best.getLengthB()) > (irh
							.getLengthA() + irh.getLengthB()))) {
				best = irh;
			}
		}

		double avg = 0;
		for (IResultHolder irh : results) {
			avg += (irh.getLengthA() + irh.getLengthB());
		}
		avg = avg / results.size();

		double stdev = 0;
		for (IResultHolder irh : results) {
			stdev += ((avg - (irh.getLengthA() + irh.getLengthB())) * (avg - (irh
					.getLengthA() + irh.getLengthB())));
		}
		stdev = stdev / results.size();
		stdev = Math.sqrt(stdev);

		System.out.println("best: " + (best.getLengthA() + best.getLengthB()));
		System.out.println("avg: " + avg);
		System.out.println("stdev: " + stdev);
		listener.solutionGenerated(best.getListA(), best.getListB(), best
				.getVertexes());

		System.out.println("-=THE END=-");
		FileOperations.saveSolutionToFile(best, "kroA150-TimeLS.txt");
	}
}
